package com.slkj.shilixiaoyuanapp.ui.teacher.tool.common;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.entity.BxDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.RepairDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolOfficialSealDetailEntity;
import com.slkj.shilixiaoyuanapp.base.BaseLazyFragment;
import com.slkj.shilixiaoyuanapp.entity.ToolCommonHistoryEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolLeaveEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolMeetingDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolOutWorkDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolPurchaseDetailEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolWplyDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.net.service.ToolService;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.OutWork.OutWorkDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.OutWork.OutWorkPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.OutWork.OutWorkWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.Purchase.PurchaseDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.Purchase.PurchasePassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.Purchase.PurchaseWtgActivity;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.Purchase.PurchaseWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.goods.GoodsDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.goods.GoodsPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.goods.GoodsWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.leave.LeaveDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.leave.LeavePassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.leave.LeaveWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.meeting.MeetingDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.meeting.MeetingPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.meeting.MeetingWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.officialSeal.SealDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.officialSeal.SealPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.officialSeal.SealWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.reimbursement.ReimbursementDspActivityActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.reimbursement.ReimbursementPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.reimbursement.ReimbursementWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.repair.RepairDspActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.repair.RepairPassedActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.repair.RepairWtgActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.MySwipeRefreshLayout;
import com.slkj.shilixiaoyuanapp.view.StateLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_BIAOXIAO;
import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_BX;
import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_CC;
import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_CG;
import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_CLSH;
import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_GZ;
import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_QJ;
import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_WPLY;
import static com.slkj.shilixiaoyuanapp.entity.code.ToolCode.TOOL_YT;
import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.PAGE_COUNT;

/**
 * Created  on 2018-07-18
 *
 * @author 唐开阔
 * @describe 行政工具-历史列表
 */
public class CommonToolHistoryFragment extends BaseLazyFragment implements SwipeRefreshLayout.OnRefreshListener {
    public static final int TYPE_ALL = -1;
    public static final int TYPE_NOT_LOOK = 0;
    public static final int TYPE_REFUSE = 1;
    public static final int TYPE_OK = 2;
    @BindView(R.id.recycer)
    RecyclerView recycerView;
    BaseQuickAdapter adapter;
    public List<ToolCommonHistoryEntity> datas = new ArrayList<>();
    public int type;
    public int toolType, netToolType;
    @BindView(R.id.layout_delete)
    LinearLayout layoutDelete;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.layout_root)
    StateLayout stateLayout;
    @BindView(R.id.swipeLayout)
    MySwipeRefreshLayout refreshLayout;
    //是否在选择删除状态
    private boolean isDeleteStatu = false;
    private int page = 1;
    private int count = PAGE_COUNT;


    @Override
    protected int getResId() {
        return R.layout.fragment_tool_common_history;
    }

    public static CommonToolHistoryFragment get(int type, int toolType, int netToolType) {
        CommonToolHistoryFragment sealHistoryFragment = new CommonToolHistoryFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        bundle.putInt("toolType", toolType);
        bundle.putInt("netToolType", netToolType);
        sealHistoryFragment.setArguments(bundle);
        return sealHistoryFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getInt("type");
            toolType = getArguments().getInt("toolType");
            netToolType = getArguments().getInt("netToolType");
        }
    }


    @Override
    public void onRealLoaded() {
        recycerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        adapter = new BaseQuickAdapter<ToolCommonHistoryEntity, BaseViewHolder>(R.layout.item_tool_outwork_history, datas) {
            @Override
            protected void convert(BaseViewHolder helper, final ToolCommonHistoryEntity item) {
                TextView typeView = helper.getView(R.id.tv_type);
                if (type == TYPE_ALL) {
                    typeView.setTextColor(item.getTypeColor());
                    typeView.setVisibility(View.VISIBLE);
                    typeView.setText(item.getTypeText());
                } else {
                    typeView.setVisibility(View.GONE);
                }
                helper.setText(R.id.tv_time, item.getIntime());

                helper.setText(R.id.tv_name, item.getTitle());
                ImageView imageView = helper.getView(R.id.img_check);
                imageView.setVisibility(isDeleteStatu ? View.VISIBLE : View.GONE);
                imageView.setImageResource(item.isCheck() ? R.drawable.ic_checked : R.drawable.ic_un_checked);
                helper.itemView.setOnClickListener(v -> {
                    if (isDeleteStatu) {
                        item.setCheck(!item.isCheck());
                        imageView.setImageResource(item.isCheck() ? R.drawable.ic_checked : R.drawable.ic_un_checked);
                    } else {
                        getDetails(item);
                    }
                });
            }

        };
        adapter.setOnLoadMoreListener(this::loadMore, recycerView);
        refreshLayout.setOnRefreshListener(this);
        stateLayout.setOnReLoadListener(this::loadFirstData);
        recycerView.setAdapter(adapter);
        checkbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            for (ToolCommonHistoryEntity item : datas) {
                item.setCheck(isChecked);
            }
            adapter.notifyDataSetChanged();
        });
        loadFirstData();
    }

    /**
     * 获取详情-判断类型在进行不同的跳转
     */
    private void getDetails(ToolCommonHistoryEntity item) {
        ToolService toolService = HttpHeper.get().toolService();
        int id = item.getId();
        switch (toolType) {
            case TOOL_YT:
                toolService.getMettingDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolMeetingDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolMeetingDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        MeetingDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        MeetingWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        MeetingPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case TOOL_GZ:
                toolService.getOfficialSealDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolOfficialSealDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolOfficialSealDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        SealDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        SealWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        SealPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case TOOL_BX:
                toolService.getRepairDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<List<RepairDetailEntity>>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(List<RepairDetailEntity> list) {
                                RepairDetailEntity data = list.get(0);
                                data.setAuditor(item.getAuditor());
                                data.setIntime(item.getIntime());
                                switch (data.getState()) {
                                    case 0:
                                        RepairDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        RepairWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        RepairPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case TOOL_QJ:
                toolService.getLeaveDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolLeaveEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolLeaveEntity data) {
                               data.setAuditor(item.getAuditor());
                               data.setIntime(item.getIntime());
                                switch (data.getState()) {
                                    case 0:
                                        LeaveDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        LeaveWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        LeavePassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case TOOL_BIAOXIAO:
                toolService.getBaoxiaoDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<List<BxDetailEntity>>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(List<BxDetailEntity> list) {
                                BxDetailEntity data = list.get(0);
                                data.setAuditor(item.getAuditor());
                                data.setIntime(item.getIntime());
                                switch (data.getState()) {
                                    case 0:
                                        ReimbursementDspActivityActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        ReimbursementWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        ReimbursementPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case TOOL_CLSH:

                break;
            case TOOL_CC:
                toolService.getCcDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolOutWorkDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolOutWorkDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        OutWorkDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        OutWorkWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        OutWorkPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case TOOL_WPLY:
                toolService.getWplyDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolWplyDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolWplyDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        GoodsDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        GoodsWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        GoodsPassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
            case TOOL_CG:
                toolService.getPurchaseDetail(id)
                        .compose(bindToLifecycle()).compose(getThread())
                        .subscribe(new CommonCallBack<ToolPurchaseDetailEntity>(true, getContext()) {
                            @Override
                            public void onCallBackSuccess(ToolPurchaseDetailEntity data) {
                                switch (data.getState()) {
                                    case 0:
                                        PurchaseDspActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 1:
                                        PurchaseWtgActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                    case 2:
                                        PurchasePassedActivity__JumpCenter.builder(getContext()).setData(data).setId(id).create().go();
                                        break;
                                }
                            }
                        });
                break;
        }
    }



    /**
     * 进入删除状态。
     */
    public void setInSeleteStatu() {
        isDeleteStatu = true;
        adapter.notifyDataSetChanged();
        layoutDelete.setVisibility(View.VISIBLE);
    }

    /**
     * 初次加载数据
     */
    private void loadFirstData() {
        refreshLayout.setEnabled(false);
        page = 1;
        HttpHeper.get().toolService()
                .getCommonHistory(page, count, getSateByType(), netToolType)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ToolCommonHistoryEntity>>(getContext()) {
                    @Override
                    public void onCallBackSuccess(List<ToolCommonHistoryEntity> data) {
                        refreshLayout.setEnabled(true);
                        stateLayout.showEmptyView();
                        setData(true, data);
                        if (data.size() == 0) {
                            stateLayout.showEmptyView();
                        } else {
                            stateLayout.showContentView();
                        }
                    }
                    @Override
                    public void onFail(String mes) {
                        refreshLayout.setRefreshing(false);
                        stateLayout.showErrorView();
                    }
                });
    }

    /**
     * 下拉刷新数据刷新数据
     */
    @Override
    public void onRefresh() {
        page = 1;
        //这里的作用是防止下拉刷新的时候还可以上拉加载
        adapter.setEnableLoadMore(false);
        HttpHeper.get().toolService()
                .getCommonHistory(page, count, getSateByType(), netToolType)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ToolCommonHistoryEntity>>(getContext()) {
                    @Override
                    public void onCallBackSuccess(List<ToolCommonHistoryEntity> data) {
                        if (data.isEmpty()) {
                            stateLayout.showEmptyView();
                        }
                        setData(true, data);

                        refreshLayout.setRefreshing(false);
                        adapter.setEnableLoadMore(true);
                        stateLayout.showContentView();

                    }

                    @Override
                    public void onFail(String mes) {
                        super.onFail(mes);
                        refreshLayout.setRefreshing(false);
                        adapter.setEnableLoadMore(true);
                        stateLayout.showErrorView();

                    }
                });
    }

    /**
     * 加载更多
     */
    private void loadMore() {
        HttpHeper.get().toolService()
                .getCommonHistory(page, count, getSateByType(), netToolType)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<List<ToolCommonHistoryEntity>>() {
                    @Override
                    public void onCallBackSuccess(List<ToolCommonHistoryEntity> data) {
                        setData(false, data);
                    }

                    @Override
                    public void onFail(String mes) {
                        adapter.loadMoreFail();
                    }
                });
    }

    private void setData(boolean isRefresh, List data) {
        page++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            adapter.setNewData(data);
        } else {
            if (size > 0) {
                adapter.addData(data);
            }
        }
        if (size < count) {
            //第一页如果不够一页就不显示没有更多数据布局
            adapter.loadMoreEnd(isRefresh);
        } else {
            adapter.loadMoreComplete();
        }
    }

    /**
     * 通过type获取网络请求的type
     *
     * @return
     */
    private String getSateByType() {
        if (type == TYPE_ALL) {
            return null;
        } else {
            return String.valueOf(type);
        }
    }

    @OnClick(R.id.tv_cancel)
    public void onTvCancelClicked() {
        layoutDelete.setVisibility(View.GONE);
        isDeleteStatu = false;
        if (checkbox.isChecked()) {
            checkbox.setChecked(false);
        }
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.tv_delete)
    public void onTvDeleteClicked(){

    }

}
